﻿SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Creating [dbo].[DataTypes]'
GO
CREATE TABLE [dbo].[DataTypes]
(
[imageField] [image] NULL,
[binaryField] [binary] (50) NULL,
[varbinaryField] [varbinary] (50) NULL,
[timestampField] [timestamp] NULL,
[textField] [text] COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_textField] DEFAULT ('a'),
[ntextField] [ntext] COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_ntextField] DEFAULT ('a'),
[charField] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_charField] DEFAULT ('a'),
[ncharField] [nchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_ncharField] DEFAULT ('a'),
[varcharField] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_varcharField] DEFAULT ('a'),
[nvarcharField] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DataTypes_nvarcharField] DEFAULT ('a'),
[xmlField] [xml] NULL,
[uniqueidentifierField] [uniqueidentifier] NULL CONSTRAINT [DF_DataTypes_uniqueidentifierField] DEFAULT (newid()),
[dateField] [date] NULL CONSTRAINT [DF_DataTypes_dateField] DEFAULT (getdate()),
[timeField] [time] NULL,
[datetime2Field] [datetime2] NULL,
[datetimeoffsetField] [datetimeoffset] NULL,
[tinyintField] [tinyint] NULL CONSTRAINT [DF_DataTypes_tinyintField] DEFAULT ((1)),
[smallintField] [smallint] NULL CONSTRAINT [DF_DataTypes_smallintField] DEFAULT ((1)),
[intField] [int] NULL CONSTRAINT [DF_DataTypes_intField] DEFAULT ((1)),
[smalldatetimeField] [smalldatetime] NULL,
[realField] [real] NULL CONSTRAINT [DF_DataTypes_realField] DEFAULT ((1)),
[moneyField] [money] NULL CONSTRAINT [DF_DataTypes_moneyField] DEFAULT ((1)),
[datetimeField] [datetime] NULL CONSTRAINT [DF_DataTypes_datetimeField] DEFAULT (getdate()),
[floatField] [float] NULL CONSTRAINT [DF_DataTypes_floatField] DEFAULT ((1)),
[sql_variantField] [sql_variant] NULL,
[bitField] [bit] NULL CONSTRAINT [DF_DataTypes_bitField] DEFAULT ((1)),
[decimalField] [decimal] (18, 0) NULL CONSTRAINT [DF_DataTypes_decimalField] DEFAULT ((1)),
[numericField] [numeric] (18, 0) NULL CONSTRAINT [DF_DataTypes_numericField] DEFAULT ((1)),
[smallmoneyField] [smallmoney] NULL CONSTRAINT [DF_DataTypes_smallmoneyField] DEFAULT ((1)),
[bigintField] [bigint] NULL CONSTRAINT [DF_DataTypes_bigintField] DEFAULT ((1)),
[hierarchyidField] [sys].[hierarchyid] NULL,
[geometryField] [sys].[geometry] NULL,
[geographyField] [sys].[geography] NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MParentA]'
GO
CREATE TABLE [dbo].[M2MParentA]
(
[M2MParentAId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MParentA] on [dbo].[M2MParentA]'
GO
ALTER TABLE [dbo].[M2MParentA] ADD CONSTRAINT [PK_M2MParentA] PRIMARY KEY CLUSTERED ([M2MParentAId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MInter]'
GO
CREATE TABLE [dbo].[M2MInter]
(
[M2MParentAId] [int] NOT NULL,
[M2MParentBId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MInter] on [dbo].[M2MInter]'
GO
ALTER TABLE [dbo].[M2MInter] ADD CONSTRAINT [PK_M2MInter] PRIMARY KEY CLUSTERED ([M2MParentAId], [M2MParentBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MParentB]'
GO
CREATE TABLE [dbo].[M2MParentB]
(
[M2MParentBId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MParentB] on [dbo].[M2MParentB]'
GO
ALTER TABLE [dbo].[M2MParentB] ADD CONSTRAINT [PK_M2MParentB] PRIMARY KEY CLUSTERED ([M2MParentBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MMChildA]'
GO
CREATE TABLE [dbo].[M2MMChildA]
(
[M2MMChildAId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MMChildA] on [dbo].[M2MMChildA]'
GO
ALTER TABLE [dbo].[M2MMChildA] ADD CONSTRAINT [PK_M2MMChildA] PRIMARY KEY CLUSTERED ([M2MMChildAId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MMInter]'
GO
CREATE TABLE [dbo].[M2MMInter]
(
[M2MMParentAId] [int] NOT NULL,
[M2MMChildId] [int] NOT NULL,
[SomeData] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MMInter] on [dbo].[M2MMInter]'
GO
ALTER TABLE [dbo].[M2MMInter] ADD CONSTRAINT [PK_M2MMInter] PRIMARY KEY CLUSTERED ([M2MMParentAId], [M2MMChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MMChildB]'
GO
CREATE TABLE [dbo].[M2MMChildB]
(
[M2MMChildBId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MMChildB] on [dbo].[M2MMChildB]'
GO
ALTER TABLE [dbo].[M2MMChildB] ADD CONSTRAINT [PK_M2MMChildB] PRIMARY KEY CLUSTERED ([M2MMChildBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MMParentA]'
GO
CREATE TABLE [dbo].[M2MMParentA]
(
[M2MMParentAId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MMParentA] on [dbo].[M2MMParentA]'
GO
ALTER TABLE [dbo].[M2MMParentA] ADD CONSTRAINT [PK_M2MMParentA] PRIMARY KEY CLUSTERED ([M2MMParentAId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MUQParentA]'
GO
CREATE TABLE [dbo].[M2MUQParentA]
(
[M2MUQParentAId] [int] NOT NULL,
[M2MUQParentASKey] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MUQParentA] on [dbo].[M2MUQParentA]'
GO
ALTER TABLE [dbo].[M2MUQParentA] ADD CONSTRAINT [PK_M2MUQParentA] PRIMARY KEY CLUSTERED ([M2MUQParentAId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating index [IX_M2MUQParentA_M2MUQParentASKey] on [dbo].[M2MUQParentA]'
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_M2MUQParentA_M2MUQParentASKey] ON [dbo].[M2MUQParentA] ([M2MUQParentASKey])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MUQInter]'
GO
CREATE TABLE [dbo].[M2MUQInter]
(
[M2MUQParentASKey] [int] NOT NULL,
[M2MUQParentBSKey] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MUQInter] on [dbo].[M2MUQInter]'
GO
ALTER TABLE [dbo].[M2MUQInter] ADD CONSTRAINT [PK_M2MUQInter] PRIMARY KEY CLUSTERED ([M2MUQParentASKey], [M2MUQParentBSKey])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2MUQParentB]'
GO
CREATE TABLE [dbo].[M2MUQParentB]
(
[M2MUQParentBId] [int] NOT NULL,
[M2MUQParentBSKey] [int] NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2MUQParentB] on [dbo].[M2MUQParentB]'
GO
ALTER TABLE [dbo].[M2MUQParentB] ADD CONSTRAINT [PK_M2MUQParentB] PRIMARY KEY CLUSTERED ([M2MUQParentBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating index [IX_M2MUQParentB_M2MUQParentBSKey] on [dbo].[M2MUQParentB]'
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_M2MUQParentB_M2MUQParentBSKey] ON [dbo].[M2MUQParentB] ([M2MUQParentBSKey])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2OChild]'
GO
CREATE TABLE [dbo].[M2OChild]
(
[M2OChildId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2OChild] on [dbo].[M2OChild]'
GO
ALTER TABLE [dbo].[M2OChild] ADD CONSTRAINT [PK_M2OChild] PRIMARY KEY CLUSTERED ([M2OChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[M2OParent]'
GO
CREATE TABLE [dbo].[M2OParent]
(
[M2OParentId] [int] NOT NULL,
[M2OChildId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_M2OParent] on [dbo].[M2OParent]'
GO
ALTER TABLE [dbo].[M2OParent] ADD CONSTRAINT [PK_M2OParent] PRIMARY KEY CLUSTERED ([M2OParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2MParent]'
GO
CREATE TABLE [dbo].[O2MParent]
(
[O2MParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2MParent_1] on [dbo].[O2MParent]'
GO
ALTER TABLE [dbo].[O2MParent] ADD CONSTRAINT [PK_O2MParent_1] PRIMARY KEY CLUSTERED ([O2MParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2MChild]'
GO
CREATE TABLE [dbo].[O2MChild]
(
[O2MChildId] [int] NOT NULL,
[O2MParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2MChild_1] on [dbo].[O2MChild]'
GO
ALTER TABLE [dbo].[O2MChild] ADD CONSTRAINT [PK_O2MChild_1] PRIMARY KEY CLUSTERED ([O2MChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2ONFKParent]'
GO
CREATE TABLE [dbo].[O2ONFKParent]
(
[O2ONFKParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2ONFKParent] on [dbo].[O2ONFKParent]'
GO
ALTER TABLE [dbo].[O2ONFKParent] ADD CONSTRAINT [PK_O2ONFKParent] PRIMARY KEY CLUSTERED ([O2ONFKParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2ONFKChild]'
GO
CREATE TABLE [dbo].[O2ONFKChild]
(
[O2ONFKChildId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2ONFKChild] on [dbo].[O2ONFKChild]'
GO
ALTER TABLE [dbo].[O2ONFKChild] ADD CONSTRAINT [PK_O2ONFKChild] PRIMARY KEY CLUSTERED ([O2ONFKChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2ONUQParent]'
GO
CREATE TABLE [dbo].[O2ONUQParent]
(
[O2ONUQParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2ONUQParent] on [dbo].[O2ONUQParent]'
GO
ALTER TABLE [dbo].[O2ONUQParent] ADD CONSTRAINT [PK_O2ONUQParent] PRIMARY KEY CLUSTERED ([O2ONUQParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2ONUQChild]'
GO
CREATE TABLE [dbo].[O2ONUQChild]
(
[O2ONUQChildId] [int] NOT NULL,
[O2ONUQParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2ONUQChild] on [dbo].[O2ONUQChild]'
GO
ALTER TABLE [dbo].[O2ONUQChild] ADD CONSTRAINT [PK_O2ONUQChild] PRIMARY KEY CLUSTERED ([O2ONUQChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2OPKChild]'
GO
CREATE TABLE [dbo].[O2OPKChild]
(
[O2OPKChildId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2OPKChild] on [dbo].[O2OPKChild]'
GO
ALTER TABLE [dbo].[O2OPKChild] ADD CONSTRAINT [PK_O2OPKChild] PRIMARY KEY CLUSTERED ([O2OPKChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[O2OPKParent]'
GO
CREATE TABLE [dbo].[O2OPKParent]
(
[O2OPKParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_O2OPKParent] on [dbo].[O2OPKParent]'
GO
ALTER TABLE [dbo].[O2OPKParent] ADD CONSTRAINT [PK_O2OPKParent] PRIMARY KEY CLUSTERED ([O2OPKParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating [dbo].[ControlParent]'
GO
CREATE TABLE [dbo].[ControlParent]
(
[ControlParentId] [int] NOT NULL
)

GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_ControlParent] on [dbo].[ControlParent]'
GO
ALTER TABLE [dbo].[ControlParent] ADD CONSTRAINT [PK_ControlParent] PRIMARY KEY CLUSTERED ([ControlParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding constraints to [dbo].[O2ONUQChild]'
GO
ALTER TABLE [dbo].[O2ONUQChild] ADD CONSTRAINT [IX_O2ONUQChild_O2ONUQ] UNIQUE NONCLUSTERED ([O2ONUQParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[M2MInter]'
GO
ALTER TABLE [dbo].[M2MInter] ADD
CONSTRAINT [FK_M2MInter_M2MParentA] FOREIGN KEY ([M2MParentAId]) REFERENCES [dbo].[M2MParentA] ([M2MParentAId]),
CONSTRAINT [FK_M2MInter_M2MParentB] FOREIGN KEY ([M2MParentBId]) REFERENCES [dbo].[M2MParentB] ([M2MParentBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[M2MMInter]'
GO
ALTER TABLE [dbo].[M2MMInter] ADD
CONSTRAINT [FK_M2MMInter_M2MMChildA] FOREIGN KEY ([M2MMChildId]) REFERENCES [dbo].[M2MMChildA] ([M2MMChildAId]),
CONSTRAINT [FK_M2MMInter_M2MMChildB] FOREIGN KEY ([M2MMChildId]) REFERENCES [dbo].[M2MMChildB] ([M2MMChildBId]),
CONSTRAINT [FK_M2MMInter_M2MMParentA] FOREIGN KEY ([M2MMParentAId]) REFERENCES [dbo].[M2MMParentA] ([M2MMParentAId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[M2MUQInter]'
GO
ALTER TABLE [dbo].[M2MUQInter] ADD
CONSTRAINT [FK_M2MUQInter_M2MUQParentA] FOREIGN KEY ([M2MUQParentASKey]) REFERENCES [dbo].[M2MUQParentA] ([M2MUQParentAId]),
CONSTRAINT [FK_M2MUQInter_M2MUQParentB] FOREIGN KEY ([M2MUQParentBSKey]) REFERENCES [dbo].[M2MUQParentB] ([M2MUQParentBId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[M2OParent]'
GO
ALTER TABLE [dbo].[M2OParent] ADD
CONSTRAINT [FK_M2OParent_M2OChild] FOREIGN KEY ([M2OChildId]) REFERENCES [dbo].[M2OChild] ([M2OChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[O2MChild]'
GO
ALTER TABLE [dbo].[O2MChild] ADD
CONSTRAINT [FK_O2MChild_O2MParent] FOREIGN KEY ([O2MParentId]) REFERENCES [dbo].[O2MParent] ([O2MParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[O2ONFKChild]'
GO
ALTER TABLE [dbo].[O2ONFKChild] ADD
CONSTRAINT [FK_O2ONFKChild_O2ONFKParent] FOREIGN KEY ([O2ONFKChildId]) REFERENCES [dbo].[O2ONFKParent] ([O2ONFKParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[O2ONUQChild]'
GO
ALTER TABLE [dbo].[O2ONUQChild] ADD
CONSTRAINT [FK_O2ONUQChild_O2ONUQParent] FOREIGN KEY ([O2ONUQParentId]) REFERENCES [dbo].[O2ONUQParent] ([O2ONUQParentId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[O2OPKParent]'
GO
ALTER TABLE [dbo].[O2OPKParent] ADD
CONSTRAINT [FK_O2OPKParent_O2OPKChild] FOREIGN KEY ([O2OPKParentId]) REFERENCES [dbo].[O2OPKChild] ([O2OPKChildId])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO